﻿using CMS.Controls.Messages;
using CMS.Controls.TabPageTemplate;
using CMS.Infos;
using CMS.REST;
using Newtonsoft.Json.Linq;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CMS.Controls.DisplayTabPages.SettingTabPages
{
    public class UserEditorTabAdapter : EditorAdaptor
    {
        public override Type ElementType => typeof(User);

        public override string ItemName => "系统用户";

        public override string API => "apis/v1/users";

        public override string PkField => "UserName";

        public override string NameField => "UserName";

        public override bool AllowIndexItem => Permission.CheckPermission(Permission.User.Index);

        public override bool AllowCreateItem => Permission.CheckPermission(Permission.User.Save);

        public override bool AllowUpdateItem => Permission.CheckPermission(Permission.User.Update);

        public override bool AllowDeleteItem => Permission.CheckPermission(Permission.User.Delete);

        private ToolTip TT_Main;
        private FormPanel FP_Main;
        private TextBox TB_UserName;
        private Button Bn_ResetPassword;
        private NumericUpDown NUD_Level;
        private CheckBox CB_Status;


        public override void InitializeEditor(Panel editorPanel, BindingSource source)
        {
            TT_Main = new ToolTip();
            FP_Main = new FormPanel();
            _SettingFormPanel(editorPanel, FP_Main);


            TB_UserName = new MyTextBox();
            TB_UserName.DataBindings.Add("Text", source, "UserName");
            FP_Main.AddField("用户名", TB_UserName);
            TT_Main.SetToolTip(TB_UserName, "用户名");

            NUD_Level = new MyNumericUpDown
            {
                Maximum = 10,
                Minimum = 0
            };
            NUD_Level.DataBindings.Add("Value", source, "Level");
            TT_Main.SetToolTip(NUD_Level, "权限等级,超级管理员的权限为0,但通过UI禁止设置超级管理员权限,\r\n普通管理员权限值为1,具有大部分功能,\r\n辅助管理员权限为2,具有助理管理等功能,\r\n普通助理权限为10\r\n赋予权限应谨慎!!!");

            FP_Main.AddField("权限等级", NUD_Level);

            CB_Status = new CheckBox { Text = "启用" };
            CB_Status.DataBindings.Add("Checked", source, "Status");
            TT_Main.SetToolTip(CB_Status, "是否启用该用户");
            FP_Main.AddField("是否启用", CB_Status);

            Bn_ResetPassword = new MyButton()
            {
                Text = "重置该用户密码"
            };

            TT_Main.SetToolTip(Bn_ResetPassword, "当用户忘记密码或新创建用户没有密码时,请使用此按钮进行密码重置,并告知用户新密码");
            Bn_ResetPassword.Click += (sender, e) =>
            {
                RESTResponse response = RESTClient.HttpPost("apis/v1/reset_password", new JObject { { "user_name", TB_UserName.Text } });
                if (response.IsSuccess)
                {
                    _SendMessage(MsgType.OK, $"为用户{TB_UserName.Text}重置密码成功");
                    MessageBox.Show( $"为用户重置密码成功,用户{TB_UserName.Text}的新密码为:{response.Data[0].ToObject<string>()},请告知用户并提醒他修改密码", "重置密码成功");
                }
                else
                {
                    _SendMessage(MsgType.OK, $"为用户{TB_UserName.Text}重置密码失败:{response.ErrMsg}");
                }
            };

            FormBase.ColumnDefinition[] definition = FP_Main.CloneColumnDefinition(false, true, false);
            definition[1].Padding.Top = definition[1].Padding.Bottom = 15;
            FP_Main.AddField("密码重置", Bn_ResetPassword, null, definition);

        }

        public override bool UpdateDisplayMode(Panel editorPanel, DisplayMode mode)
        {
            switch (mode)
            {
                case DisplayMode.New:
                    Bn_ResetPassword.Enabled = false;
                    TB_UserName.Enabled = true;
                    NUD_Level.Enabled =CB_Status.Enabled= true;
                    break;
                case DisplayMode.Edit:
                    Bn_ResetPassword.Enabled = true;
                    TB_UserName.Enabled = false;
                    NUD_Level.Enabled = CB_Status.Enabled = true;
                    break;
                case DisplayMode.Preview:
                    Bn_ResetPassword.Enabled = false;
                    TB_UserName.Enabled = false;
                    NUD_Level.Enabled = CB_Status.Enabled = false;
                    break;
                case DisplayMode.None:
                    FP_Main.ClearContent();
                    break;
                default:
                    return false;
            }
            return true;
        }

        public override bool ValidateForm()
        {
            return FP_Main.Validate();
        }
    }
}
